package core.jfinal.ext.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/3/17.
 */
public class TestBaseController  extends Controller{
    private static String REGEX = "(([a-zA-Z][a-zA-Z0-9]*(\\[[0-9]+\\])?)\\.)*([a-zA-Z][a-zA-Z0-9]*(\\[[0-9]+\\])?)";


    public Record getParaData1(){
        Map<String, Object> map = new HashMap();
        Map<String, String[]> paraMap = getParaMap();
        Iterator i$ = paraMap.entrySet().iterator();

        while(i$.hasNext()) {
            Map.Entry entry = (Map.Entry) i$.next();
            String key = (String) entry.getKey();
            String[] value = (String[]) entry.getValue();
            if(!key.contains(".")){
                if(value.length==1)
                    map.put(key, value[0]);
                else
                    map.put(key, Arrays.asList(value));
            } else {
                String[] k1Andk2 = key.split("\\.");
                if(k1Andk2.length != 2) throw new RuntimeException("["+key+"]格式不支持");
                String k1 = k1Andk2[0];
                String k2 = k1Andk2[1];
                if(map.get(k1) == null) {
                    map.put(k1, new ArrayList<Map>());
                }
                List<Map> k1List = (List) map.get(k1);
                for(int i =0;i<value.length;i++) {
                    if(k1List.size()>i) {
                        k1List.get(i).put(k2, value[i]);
                    } else {
                        Map k2Map = new HashMap();
                        k2Map.put(k2, value[i]);
                        k1List.add(k2Map);
                    }
                }
            }
        }
        return new Record().setColumns(map);
    }

    public Record getParaData2(){
        Map<String, Object> root = new HashMap();
        Map<String, String[]> paraMap = getParaMap();
        Iterator i$ = paraMap.entrySet().iterator();


        while(i$.hasNext()) {
            Map.Entry entry = (Map.Entry) i$.next();
            String key = (String) entry.getKey();
            String[] value = (String[]) entry.getValue();

            //解析key
            if(!isTrueKey(key)) {
                throw new RuntimeException("["+key+"]格式不匹配");
            }
            Object tempRoot = root;
            Pattern p = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)(\\[([0-9]+)\\])?\\.?");
            Matcher m2 = p.matcher(key);

            while(m2.find()) {
                System.out.println(m2.group(1));//dada
                System.out.println(m2.group(3));//213
                String k = m2.group(1);
                Integer arrayIndex = Integer.parseInt(m2.group(3));
                if(arrayIndex == null) {//Map的情况
                    if(m2.hitEnd()) {
                        ((Map)tempRoot).put(k, value.length==1?value[0]:Arrays.asList(value));
                        break;
                    }
                    if(((Map)tempRoot).get(k) == null){
                        ((Map)tempRoot).put(k, new HashMap());
                    }
                    tempRoot = ((Map)tempRoot).get(k);
                } else {//List的情况

                    if(((Map)tempRoot).get(k) == null){
                        List list = new ArrayList();
                        for(int i=0;i<=arrayIndex;i++)
                            list.add(null);
                        ((Map)tempRoot).put(k, new ArrayList());
                    }
                    tempRoot = ((Map)tempRoot).get(k);
                    if(m2.hitEnd()){
                        ((List)tempRoot).set(arrayIndex,value.length==1?value[0]:Arrays.asList(value));
                    }


                }

            }
        }


        return null;
    }



    /*public Record getParaData(){
        Map<String, String[]> paraMap = getParaMap();
        Map<String, Object> root = new HashMap();
        Iterator i$ = paraMap.entrySet().iterator();


        while(i$.hasNext()) {
            Map.Entry entry = (Map.Entry) i$.next();
            String key = (String) entry.getKey();
            String[] value = (String[]) entry.getValue();

            //解析key
            if(!isTrueKey(key)) {
                throw new RuntimeException("["+key+"]格式不匹配");
            }
            Object tempRoot = root;
            Pattern p = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)(\\[([0-9]+)\\])?\\.?");
            Matcher m2 = p.matcher(key);

            while(m2.find()) {
                System.out.println(m2.group(1));//dada
                System.out.println(m2.group(3));//213
                String k = m2.group(1);
                Integer arrayIndex = null;
                if(m2.group(3) != null)
                    arrayIndex = Integer.parseInt(m2.group(3));
                if(arrayIndex == null) {//Map的情况
                    if(m2.hitEnd()) {
                        ((Map)tempRoot).put(k, value.length==1?value[0]:Arrays.asList(value));
                        break;
                    }
                    if(((Map)tempRoot).get(k) == null){
                        ((Map)tempRoot).put(k, new HashMap<>());
                    }
                    tempRoot = ((Map)tempRoot).get(k);
                } else {//List的情况
                    List list = (List)(((Map)tempRoot).get(k));
                    if(list == null){
                        List arr = new ArrayList();
                        for(int i=0;i<=arrayIndex;i++)
                            arr.add(new HashMap<>());
                        ((Map)tempRoot).put(k, arr);
                    } else if(list.size() <= arrayIndex){
                        for(int i=list.size();i<=arrayIndex;i++)
                            list.add(new HashMap<>());
                    }
                    if(m2.hitEnd()){
                        list.set(arrayIndex, value.length==1?value[0]:Arrays.asList(value));
                        break;
                    }
                    list = (List)(((Map)tempRoot).get(k));
                    tempRoot = list.get(arrayIndex);


                }

            }
        }
        return new Record().setColumns(root);
    }*/


    private static boolean isTrueKey(String key) {
        //匹配格式 (([a-zA-Z][a-zA-Z0-9]*(\[[0-9]+\])?)\.)*([a-zA-Z][a-zA-Z0-9]*(\[[0-9]+\])?)
        Pattern p = Pattern.compile(REGEX);
        Matcher m1 = p.matcher(key);

        return m1.matches();



    }

    public static void main(String[] args) {
        /*Map<String, String[]> paraMap = new HashMap<>();
        *//*paraMap.put("a[0].b[1]", new String[]{"1","2"});
        paraMap.put("a[0].a", new String[]{"11","22"});
        paraMap.put("a[1].b", new String[]{"3","4"});
        paraMap.put("a[1].a", new String[]{"33","44"});
        paraMap.put("b[0].bb", new String[]{"33","44"});
        paraMap.put("b[1].bb", new String[]{"33","44"});
        paraMap.put("b[2].bb", new String[]{"33","44"});
        paraMap.put("c.v.b.n", new String[]{"44"});*//*
        paraMap.put("id[0]", new String[]{"44"});
        paraMap.put("a[0]", new String[]{"44"});


        Map<String, Object> root = new HashMap();
        Iterator i$ = paraMap.entrySet().iterator();


        while(i$.hasNext()) {
            Map.Entry entry = (Map.Entry) i$.next();
            String key = (String) entry.getKey();
            String[] value = (String[]) entry.getValue();

            //解析key
            if(!isTrueKey(key)) {
                throw new RuntimeException("["+key+"]格式不匹配");
            }
            Object tempRoot = root;
            Pattern p = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)(\\[([0-9]+)\\])?\\.?");
            Matcher m2 = p.matcher(key);

            while(m2.find()) {
                System.out.println(m2.group(1));//dada
                System.out.println(m2.group(3));//213
                String k = m2.group(1);
                Integer arrayIndex = null;
                if(m2.group(3) != null)
                    arrayIndex = Integer.parseInt(m2.group(3));
                if(arrayIndex == null) {//Map的情况
                    Map map = (Map) ((Map)tempRoot).get(k);
                    if(m2.hitEnd()) {
                        ((Map)tempRoot).put(k, value.length==1?value[0]:Arrays.asList(value));
                        break;
                    }
                    if(((Map)tempRoot).get(k) == null){
                        ((Map)tempRoot).put(k, new HashMap<>());
                        map = (Map) ((Map)tempRoot).get(k);
                    }
                    tempRoot = map;
                } else {//List的情况
                    List list = (List)(((Map)tempRoot).get(k));
                    if(list == null){
                        List arr = new ArrayList();
                        for(int i=0;i<=arrayIndex;i++)
                            arr.add(new HashMap<>());
                        ((Map)tempRoot).put(k, arr);
                        list = (List)(((Map)tempRoot).get(k));
                    } else if(list.size() <= arrayIndex){
                        for(int i=list.size();i<=arrayIndex;i++)
                            list.add(new HashMap<>());
                    }
                    if(m2.hitEnd()){
                        list.set(arrayIndex, value.length==1?value[0]:Arrays.asList(value));
                        break;
                    }

                    tempRoot = list.get(arrayIndex);


                }

            }
        }

        System.out.println("");
        Pattern p = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)(\\[([0-9]+)\\])?\\.?");
        Matcher matcher = p.matcher("dada[2].a1[3].f.v.a.da.r.d");

        while(matcher.find()){
            System.out.println(matcher.group(1));
            System.out.println(matcher.group(2));
            System.out.println(matcher.group(3));
            System.out.println(matcher.hitEnd());

            System.out.println("-----");
        }

        List list = new ArrayList();
        for(int i=0;i<=11;i++)
            list.add(null);
        System.out.println("da");

        //(([a-zA-Z][a-zA-Z0-9]*(\[[0-9]+\])?)\.)*([a-zA-Z][a-zA-Z0-9]*(\[[0-9]+\])?)
        System.out.println(matcher.matches());
        System.out.println(matcher.group());
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
        System.out.println(matcher.group(4));
        System.out.println(matcher.group(5));
        System.out.println(matcher.groupCount());
*/
        String reg = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*";


        Pattern pp = Pattern.compile(reg);
        Matcher mm = pp.matcher("aA111111");
        System.out.println(mm.matches());
        System.out.println(mm.find());
        System.out.println(mm.find());
        System.out.println(mm.find());
    }

}
